package com.hjb.toolbardemo.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hjb.toolbardemo.R;
import com.hjb.toolbardemo.inter.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 : hjb
 * 时间 : 2016/5/12.
 */
public abstract class RecyclerBaseAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    public Context context;
    public List<T> data = new ArrayList<>();
    public LayoutInflater mInflater;
    public OnRecyclerViewItemClickListener itemClickListener;
    public View rootView;
    private ViewGroup parent;
    private int viewType;

    public RecyclerBaseAdapter(Context context, List<T> data) {
        super();
        this.context = context;
        if (data != null) {
            this.data = data;
        }
        mInflater = LayoutInflater.from(context);
    }

    public void setOnRecyclerViewItemClickListener(final OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        itemClickListener = onRecyclerViewItemClickListener;
    }

    protected int getPosition(RecyclerView.ViewHolder viewHolder) {
        return viewHolder.getAdapterPosition();
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        rootView = getItemView();
        this.parent = parent;
        this.viewType = viewType;
        return new BaseRecyclerViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, int position) {
        setListener(parent, position, holder, viewType);
        onBindViewHolder_(holder, position);
    }


    protected boolean isEnabled(int viewType) {
        return true;
    }

    protected void setListener(final ViewGroup parent, final int position, final BaseRecyclerViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) return;
        if (rootView != null) {
            rootView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (itemClickListener != null) {
                        return itemClickListener.onItemLongClick(parent, v, data.get(position), position);
                    }
                    return false;
                }
            });
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(parent, v, data.get(position), position);
                    }
                }
            });
        }
    }


    protected abstract void onBindViewHolder_(BaseRecyclerViewHolder holder, int position);

    public abstract View getItemView();

    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }

    public void append(List<T> list) {
        if (this.data != null) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void appendAll(List<T> list) {
        if (this.data != null) {
            this.data.clear();
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        } else {
            return 0;
        }
    }


}
